#include <cstdlib>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};


class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        ListNode* node1 = head;
        ListNode* node2 = head;
        int count = 0;
        while (node2 != nullptr) {
            node2 = node2->next;
            if (count < k) {
                count++;
            } else {
                node1 = node1->next;
            }
        }
        return count < k ? nullptr : node1;
    }
};
